//
//  ShopListController.m
//  waimai
//
//  Created by Mac on 11-12-30.
//  Copyright (c) 2011年 waimai. All rights reserved.
//

#import "ShopListController.h"
#import "CustomCell.h"
@implementation ShopListController
@synthesize typePickerView;
@synthesize addressPickerView;
@synthesize shopHoursPickerView;
@synthesize typeTestData;
@synthesize addressTestData;
@synthesize shopHoursTestData;
@synthesize sectionBtn;
@synthesize typeBtn;
@synthesize timeBtn;
@synthesize tableViews;
#define addressTag 1//地址按钮标签
#define typeTag 2//类型按钮标签
#define shopHoursTag 3//营业时间标签

- (void)viewDidLoad {
    //初始化3个pickerView控件
    [self addAddressPickerView];
    [self addTypePickerView];
    [self addShopHoursPickerView];
    //导入测试数据
    NSArray *addressArray = [[NSArray alloc] initWithObjects:@"路段",@"湖滨路", @"九二路", nil];
    self.addressTestData=addressArray;
    NSArray *typeArray = [[NSArray alloc] initWithObjects:@"类型",@"小吃", @"冰品",@"面食", nil];
    self.typeTestData=typeArray;
    NSArray *shopHoursArray = [[NSArray alloc] initWithObjects:@"营业时间",@"白天", @"晚上",@"中午",@"24小时", nil];
    self.shopHoursTestData=shopHoursArray; 
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated{
    //设置显示导航栏
    self.navigationController.navigationBarHidden=NO; 
}

  /*************************/ 
 /*       商家列表视图       */
/**************************/

//返回表试图分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

//设置表试图每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    }
    [[cell shopNameLbl] setText:@"冰岛茶餐厅"];
//    [[cell shopTypeLbl] setText:@"30元起送，茶餐厅"];
//    [[cell shopAddressLbl] setText:@"人民路35号"];
//    [[cell shopIntroductionLbl] setText:@"石狮第一家茶餐厅，主要卖鸡翅。。"];
    //设置商家图片
//    UIImage *buttonImageNormal = [UIImage imageNamed:@"seven.png"];
//    [sho setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
    return cell;
}

//设置表试图行高
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath   
{
    return 90;
}

  /************************/ 
 /*     索搜类型选取器      */
/************************/

//用代码添加addressPickerView控件,初始位置在屏幕外
-(void) addAddressPickerView {
	if (addressPickerView == nil) {
		addressPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 460, 320, 460)];
		addressPickerView.delegate = self;
		addressPickerView.dataSource = self;
		addressPickerView.showsSelectionIndicator = YES;
		[self.view addSubview:addressPickerView];
		[addressPickerView release];
	}
}

//用代码添加typePickerView控件,初始位置在屏幕外
-(void) addTypePickerView {
	if (typePickerView == nil) {
		typePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 460, 320, 460)];
		typePickerView.delegate = self;
		typePickerView.dataSource = self;
		typePickerView.showsSelectionIndicator = YES;
		[self.view addSubview:typePickerView];
		[typePickerView release];
	}
}

//用代码添加shopHoursPickerView控件,初始位置在屏幕外
-(void) addShopHoursPickerView {
	if (shopHoursPickerView == nil) {
		shopHoursPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 460, 320, 460)];
		shopHoursPickerView.delegate = self;
		shopHoursPickerView.dataSource = self;
		shopHoursPickerView.showsSelectionIndicator = YES;
		[self.view addSubview:shopHoursPickerView];
		[shopHoursPickerView release];
	}
}

//显示路段选取器
-(void) showAddressPickerView {
    /************************/ 
    /*     选取器动画效果      */
    /************************/
    //显示路段选取器，隐藏其他
    //开始动画
    [UIView beginAnimations: @"Animation" context:nil];
    //设定动画持续时间
    [UIView setAnimationDuration:0.3]; 
    //生成选取器，并向上拉动
    addressPickerView.frame = CGRectMake(0, 240, 320, 460);
    //动画结束
    [UIView commitAnimations]; 
    
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    typePickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations]; 
    
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    shopHoursPickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations];
}

//显示类型选取器
-(void) showTypePickerView {
    //显示类型选取器，隐藏其他
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    addressPickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations]; 
    
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    typePickerView.frame = CGRectMake(0, 240, 320, 460);
    [UIView commitAnimations]; 
    
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    shopHoursPickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations];
}

//显示营业时间选取器
-(void) showShopHoursPickerView {
    //显示营业时间选取器，隐藏其他
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    addressPickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations]; 
    
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    typePickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations]; 
    
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    shopHoursPickerView.frame = CGRectMake(0, 240, 320, 460);
    [UIView commitAnimations];
}

//隐藏全部选取器
-(void) hiddenPickerView {
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    addressPickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations]; 
    
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    typePickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations]; 
    
    [UIView beginAnimations: @"Animation" context:nil];
    [UIView setAnimationDuration:0.3]; 
    shopHoursPickerView.frame = CGRectMake(0, 460, 320, 460);
    [UIView commitAnimations];
}


//点击显示选取器
- (IBAction)showPickerViewAction:(id)sender {
    //取得按钮tag
    NSInteger type=[sender tag];    
    if(type==addressTag)
    {
        [self showAddressPickerView];
    }
    else if(type==typeTag)
    {
        [self showTypePickerView];
    }
    else
    {
        [self showShopHoursPickerView];
    }
}

//返回每个组件上的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(pickerView==addressPickerView)
    {
        return [addressTestData count];
    }
    else if(pickerView==typePickerView) 
    {
        return [typeTestData count];
    }
    else
    {
        return [shopHoursTestData count];
    }

}

//返回有pickerview的组件数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//pickerview的值
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if(pickerView==addressPickerView)
    {
        return [addressTestData objectAtIndex:row];
    }
    else if(pickerView==typePickerView)
    {
        return [typeTestData objectAtIndex:row];
    }
    else
    {
        return [shopHoursTestData objectAtIndex:row];
    }
}

//当你选中pickerview的某行时会调用该函数。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView==addressPickerView)
    {
        NSInteger rows = [addressPickerView selectedRowInComponent:0];
        NSString *strs = [NSString stringWithFormat:@"%@",[addressTestData objectAtIndex:rows]];
        sectionBtn.titleLabel.text=strs;
    }
    else if(pickerView==typePickerView)
    {
        NSInteger rows = [typePickerView selectedRowInComponent:0];
        NSString *strs = [NSString stringWithFormat:@"%@",[typeTestData objectAtIndex:rows]];
        typeBtn.titleLabel.text=strs;
    }
    else
    {
        NSInteger rows = [shopHoursPickerView selectedRowInComponent:0];
        NSString *strs = [NSString stringWithFormat:@"%@",[shopHoursTestData objectAtIndex:rows]];
        timeBtn.titleLabel.text=strs;
    }
}

- (IBAction)backgroundTap:(id)sender {
    [self hiddenPickerView];
}
    
//自定义pickerview使内容显示在每行的中间，默认显示在每行的左边（(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component）
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
//	if (row == 0) {
//        label.text=@"nan";
//	}else {
//		label.text = @"女";
//	}
//	[label setTextAlignment:UITextAlignmentCenter];
//    return label;
//}



//设置指定组件上每行的宽度
/*
 -(void)pickerView:(UIPickerView *)thePickerView widthForComponet:component {
 
 }
 */

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [self setAddressTestData:nil];
    [self setTypeTestData:nil];
    [self setShopHoursTestData:nil];
    [self setTypePickerView:nil];
    [self setAddressPickerView:nil];
    [self setShopHoursPickerView:nil];
    [self setTimeBtn:nil];
    [self setTypeBtn:nil];
    [self setSectionBtn:nil];
}

- (void)dealloc {
    [addressTestData release];
    [typeTestData release];
    [shopHoursTestData release];
    [self.tableViews release];
    [sectionBtn release];
    [typeBtn release];
    [timeBtn release];
    [addressPickerView release];
    [typePickerView release];
    [shopHoursPickerView release];
    [super dealloc];
}









@end
